import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;

import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;

import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
import org.xml.sax.SAXException;

public class DOM_lesen {

	
	public void lesen() {
		// DOM ERSTELLEN
		    Document dokument=null;
			DocumentBuilderFactory fabrik= DocumentBuilderFactory.newInstance();
			fabrik.setNamespaceAware(true);
			DocumentBuilder dokumentBauer = null;
			try {
 					dokumentBauer = fabrik.newDocumentBuilder();
 					InputStream inputstream = new FileInputStream("test.xml");
 					dokument = dokumentBauer.parse(inputstream);		
			} catch (Exception e) {
					e.printStackTrace();
			}

// Wurzelelement = terminkalender einlesen + Attribut "von" ausgeben
			Element root = dokument.getDocumentElement();
			System.out.println("Terminkalender von:"+root.getAttribute("von"));
			
// <Termine> einlesen und Art ausgeben			
			Node termine_node = root.getElementsByTagName("termine").item(0);
			Element termine = (Element) termine_node;
			System.out.println("Terminart:" + termine.getAttribute("art"));
			Node kongresstermine_node = root.getElementsByTagName("termine").item(1);
			Element kongresstermine = (Element) kongresstermine_node;
			System.out.println("Terminart:" + kongresstermine.getAttribute("art"));
			
// Terminliste aus Termin auslesen
			NodeList termin_nodelist = termine.getElementsByTagName("termin");
			NodeList kongresstermin_nodelist = kongresstermine.getElementsByTagName("termin");
			
// Jeden Termin aus Terminliste auslesen und ausgeben
			for (int i=0;i<termin_nodelist.getLength();i++) {
				Node termin_node= termin_nodelist.item(i);
				Element termin = (Element) termin_node;
				    System.out.println("Termin Nr:"+termin.getAttribute("nr"));      // Termin Attribut
				    Element was=(Element)termin.getElementsByTagName("was").item(0); // Termin Element "was"
					System.out.println("Termin-Was:"+was.getTextContent());
			}
			
			for (int i = 0; i < kongresstermin_nodelist.getLength(); i++) {
				Node termin_node = kongresstermin_nodelist.item(i);
				Element termin = (Element) termin_node;
				    System.out.println("Termin Nr:" + termin.getAttribute("nr"));      // Termin Attribut
				    Element was=(Element)termin.getElementsByTagName("was").item(0); // Termin Element "was"
					System.out.println("Termin-Was:" + was.getTextContent());
					Element wo = (Element) termin.getElementsByTagName("wo").item(0);
					System.out.println("termin wo:" + wo.getTextContent());
			}
	}
			/*
			element.getAttribute("attributeName");  // Attribut eines Elements
			NodeList subelemente = element.getElementsByTagName("subelementName"); 
			Node knoten =subelemente(i);
			Element element = (Element) knoten;
			Element x = element.getElementsByTagName(ELEMENT_SCHUELERLISTE).item[0];
			x.getTextContent();
*/
			

	}

